package com.example.yuanann.vegetable_app;

import com.example.yuanann.vegetable_app.constant.UserTypeEnum;
import com.example.yuanann.vegetable_app.repository.AppDatabase;
import com.example.yuanann.vegetable_app.repository.dao.UserDao;
import com.example.yuanann.vegetable_app.repository.entity.User;
import com.example.yuanann.vegetable_app.util.GsonUtil;
import com.example.yuanann.vegetable_app.util.LogUtil;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;


import org.junit.Test;


public class UserDaoTest extends AndroidBaseTest{

    @Test
    public void insertUserTest() throws Exception{
        AppDatabase db = AppDatabase.getDatabase(getApplicationContext());
        UserDao userDao = db.userDao();
        User user = new User();
        user.setUserName("admin");
        user.setPassword("admin");
        user.setUserType(UserTypeEnum.MERCHANT.getCode());
        userDao.insert(user)
                .subscribeOn(Schedulers.single())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(()->{
                    LogUtil.info("插入数据成功");
                    userDao.getOneByUserNamePassword(user.getUserName(),user.getPassword())
                            .subscribeOn(Schedulers.single())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(item -> {
                                LogUtil.info(GsonUtil.gsonString(item));
                            },throwable -> {
                                LogUtil.info(throwable.toString());
                            });
                },throwable -> {
                    LogUtil.info("插入数据失败");
                });
    }

    @Test
    public void updateUser(){
        AppDatabase db = AppDatabase.getDatabase(getApplicationContext());
        UserDao userDao = db.userDao();
        User user = new User();
        user.setUserName("admin");
        user.setPassword("admin");
        user.setUserType(UserTypeEnum.MERCHANT.getCode());
        userDao.insert(user)
                .subscribeOn(Schedulers.single())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(()->{
                    LogUtil.info("插入数据成功");
                    user.setUserId(1);
                    user.setPassword("123456");
                    // 默认也会更新
                    user.setUserType("");
                    userDao.update(user)
                            .subscribeOn(Schedulers.single())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe((count)->{
                                LogUtil.info("更新数据成功: " + count);
                                userDao.getOneByUserNamePassword(user.getUserName(),user.getPassword())
                                        .subscribeOn(Schedulers.single())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(item -> {
                                            LogUtil.info(GsonUtil.gsonString(item));
                                        },throwable -> {
                                            LogUtil.info(throwable.toString());
                                        });
                            },throwable -> {
                                LogUtil.info(throwable.toString());
                            });
                },throwable -> {
                    LogUtil.info("插入数据失败");
                });
    }

    @Test
    public void deleteUser(){
        AppDatabase db = AppDatabase.getDatabase(getApplicationContext());
        UserDao userDao = db.userDao();
        User user = new User();
        user.setUserName("admin");
        user.setPassword("admin");
        user.setUserType(UserTypeEnum.MERCHANT.getCode());
        userDao.insert(user)
                .subscribeOn(Schedulers.single())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(()->{
                    LogUtil.info("插入数据成功");
                },throwable -> {
                    LogUtil.info("插入数据失败");
                });

        userDao.getOneByUserNamePassword(user.getUserName(),user.getPassword())
                .subscribeOn(Schedulers.single())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(item -> {
                    LogUtil.info(GsonUtil.gsonString(item));
                    userDao.delete(item)
                            .subscribeOn(Schedulers.single())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe((count)->{
                                LogUtil.info("删除数据成功: " + count);
                                userDao.getAll()
                                        .subscribeOn(Schedulers.single())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(users -> {
                                            LogUtil.info(GsonUtil.gsonString(users));
                                        },throwable -> {
                                            LogUtil.info(throwable.toString());
                                        });
                            },throwable -> {
                                LogUtil.info(throwable.toString());
                            });
                },throwable -> {
                    LogUtil.info(throwable.toString());
                });
    }
}
